import { useRoute, createRouter, createWebHashHistory, RouteRecordRaw, createWebHistory } from "vue-router";

import baseRouters from './modules/base';
import userRouters from './modules/user';
import productRouters from './modules/product';
import protoRouters from './modules/proto';
import permissionRoutes from './modules/permission';
import showcaseRouters from './modules/showcase';
import helperRouters from './modules/helper';
import securityRoutes from './modules/security';
import settingRoutes from './modules/setting';
import resultRoutes from './modules/result';

// 关于单层路由，meta 中设置 { single: true } 即可为单层路由，{ hidden: true } 即可在侧边栏隐藏该路由

// 存放动态路由
export const asyncRouterList: Array<RouteRecordRaw> = [
  ...baseRouters,
  ...productRouters,
  ...protoRouters,
  ...showcaseRouters,
  ...userRouters,
  ...permissionRoutes,
  ...helperRouters,
  ...securityRoutes,
  ...settingRoutes,
  ...resultRoutes,
];

// 存放固定的路由
const defaultRouterList: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/pages/login/index.vue'),
  },
  {
    path: '/',
    redirect: '/dashboard/base',
    component: () => import('@/layouts/blank.vue'),
  },
  {
    path: '/:w+',
    name: '404Page',
    redirect: '/result/404',
  },
];

export const allRoutes = [...defaultRouterList, ...asyncRouterList];

export const getActive = (maxLevel = 3): string => {
  const route = useRoute();
  if (!route.path) {
    return '';
  }
  return route.path
    .split('/')
    .filter((_item: string, index: number) => index <= maxLevel && index > 0)
    .map((item: string) => `/${item}`)
    .join('');
};

const router = createRouter({
  history: createWebHistory('/'),
  routes: allRoutes,
  scrollBehavior() {
    return {
      el: '#app',
      top: 0,
      behavior: 'smooth',
    };
  },
});

export default router;
